package com.example.zcapi;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ycuwq.datepicker.date.DatePicker;
import com.ycuwq.datepicker.time.HourAndMinutePicker;

import java.util.Calendar;



public class TimeSettingDialog extends Dialog{

    private String TAG = "TimeSettingDialog";
    private Context context;
//    private DatePicker datePicker;
    private HourAndMinutePicker hourAndMinutePicker;
    private int setType = 0;

    private int type = 0;
    private int startYear,startMonth,startDay,startHour,startMinute,endYear,endMonth,endDay,endHour,endMinute;
//    private int startHour,startMinute,endHour,endMinute;
    private int currentYear = 0;
    private int currentMonth = 0;
    private int currentDay = 0;
    private int currentHour = 0;
    private int currentMinute = 0;
    private long currentMillis = 0L;
    private long tomorrowMillis = 0L;
    private OnTimeListener onTimeListener;

    public OnTimeListener getOnTimeListener() {
        return onTimeListener;
    }

    public void setOnTimeListener(OnTimeListener onTimeListener) {
        this.onTimeListener = onTimeListener;
    }

    public TimeSettingDialog(Context context,int setType) {
        super(context, R.style.ScheduleExitDialog);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.setType = setType;
    }

    //用于控制失去焦点后是否消失
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.time_setting_dialog_v_layout);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(true);
        setCancelable(true);

//        datePicker = findViewById(R.id.datePicker);
        hourAndMinutePicker = findViewById(R.id.timePicker);

        if(setType == 0){
            ((TextView)findViewById(R.id.titleTv)).setText("请设置关机和开机时间");
            ToastUtils.showShort("请设置关机时间");
        }else  {
            ((TextView)findViewById(R.id.titleTv)).setText("请设置取消关机和开机时间");
            ToastUtils.showShort("请设置取消关机时间");
        }


        findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar currentCalender = Calendar.getInstance();
                currentYear = currentCalender.get(Calendar.YEAR);
                currentMonth = currentCalender.get(Calendar.MONTH) + 1;
                currentDay = currentCalender.get(Calendar.DAY_OF_MONTH);
                currentHour = currentCalender.get(Calendar.HOUR_OF_DAY);
                currentMinute = currentCalender.get(Calendar.MINUTE);
                currentMillis = currentCalender.getTimeInMillis();

                LogUtils.d(TAG,"checkPowerOnOff TimeSettingDialog current" + currentYear + " " + currentMonth + " " + currentDay);

                if(type == 0){

                    type = 1;

                    endYear = currentYear;
                    endMonth =  currentMonth;
                    endDay = currentDay;
                    endHour = hourAndMinutePicker.getHour();
                    endMinute = hourAndMinutePicker.getMinute();
//                    ToastUitls.showShortToast(context,"请设置开机时间");

                    findViewById(R.id.powerOnTv).setBackgroundResource(R.drawable.corners_edit_blue_bg);
                    ((TextView)findViewById(R.id.powerOnTv)).setTextColor(ResourceUtil.getColor(R.color.white));
                    findViewById(R.id.powerOffTv).setBackgroundResource(R.drawable.corners_edit_gery_bg_5);
                    ((TextView)findViewById(R.id.powerOffTv)).setTextColor(ResourceUtil.getColor(R.color.svg_blue));

                }else if(type == 1){

//                    startYear = datePicker.getYear();
//                    startMonth =  datePicker.getMonth();
//                    startDay = datePicker.getDay();
                    startHour = hourAndMinutePicker.getHour();
                    startMinute = hourAndMinutePicker.getMinute();

                    tomorrowMillis = currentMillis + 3600 * 1000 * 24;
                    Calendar tomorrow = Calendar.getInstance();
                    tomorrow.setTimeInMillis(tomorrowMillis);

                    if ((currentHour == startHour && currentMinute >= startMinute) || (currentHour > startHour)) {
                        //明天
                        startYear = tomorrow.get(Calendar.YEAR);
                        startMonth = tomorrow.get(Calendar.MONTH) + 1;
                        startDay = tomorrow.get(Calendar.DAY_OF_MONTH);
                        LogUtils.d(TAG,"checkPowerOnOff TimeSettingDialog" + startYear + " " + startMonth + " " + startDay);


                    } else {
                        //今天
                        startYear = currentYear;
                        startMonth = currentMonth;
                        startDay = currentDay;

                        LogUtils.d(TAG,"checkPowerOnOff TimeSettingDialog today" + startYear + " " + startMonth + " " + startDay);
                    }

                    if ((currentHour == endHour && currentMinute >= endMinute) || (currentHour > endHour)) {
                        //明天
                        endYear = tomorrow.get(Calendar.YEAR);
                        endMonth = tomorrow.get(Calendar.MONTH) + 1;
                        endDay = tomorrow.get(Calendar.DAY_OF_MONTH);
                    } else {
                        //今天
                        endYear = currentYear;
                        endMonth = currentMonth;
                        endDay = currentDay;
                    }

                    int []onTime={startYear,startMonth,startDay,startHour,startMinute};
                    int []offTime={endYear,endMonth,endDay,endHour,endMinute};

//                    long nowTimes = System.currentTimeMillis();
//
//                    Calendar onTimes = Calendar.getInstance();
//                    onTimes.set(startYear, startMonth-1, startDay, startHour, startMinute, 0);
//                    long onTimestamp = onTimes.getTimeInMillis();
//
//                    Calendar offTimes = Calendar.getInstance();
//                    offTimes.set(endYear, endMonth-1, endDay, endHour, endMinute, 0);
//                    long offTimestamp = offTimes.getTimeInMillis();

                    if(onTimeListener!=null){
                        if(setType == 0){
                            onTimeListener.powerChange(onTime,offTime);
                        }else {
                            onTimeListener.cancelPowerChange(onTime,offTime);
                        }
                    }

//                    AppConfig.setPowerOffYear(endYear);
//                    AppConfig.setPowerOffMonth(endMonth);
//                    AppConfig.setPowerOffDay(endDay);
//                    AppConfig.setPowerOffHour(endHour);
//                    AppConfig.setPowerOffMinute(endMinute);
//
//                    AppConfig.setPowerOnYear(startYear);
//                    AppConfig.setPowerOnMonth(startMonth);
//                    AppConfig.setPowerOnDay(startDay);
//                    AppConfig.setPowerOnHour(startHour);
//                    AppConfig.setPowerOnMinute(startMinute);
//
//                    AppConfig.setPowerOff(endYear + "/" +  endMonth + "/" + endDay + " " + endHour + ":" + endMinute);
//                    AppConfig.setPowerOn(startYear + "/" +  startMonth + "/" + startDay + " " + startHour + ":" + startMinute);
//                    ToastUtils.showShort("设置成功");
                    dismiss();
                }
            }
        });

        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(type == 1){
                    type = 0 ;
                    findViewById(R.id.powerOffTv).setBackgroundResource(R.drawable.corners_edit_blue_bg);
                    ((TextView)findViewById(R.id.powerOffTv)).setTextColor(ResourceUtil.getColor(R.color.white));
                    findViewById(R.id.powerOnTv).setBackgroundResource(R.drawable.corners_edit_gery_bg_5);
                    ((TextView)findViewById(R.id.powerOnTv)).setTextColor(ResourceUtil.getColor(R.color.svg_blue));

                    ToastUtils.showShort("请设置关机时间");
                }else {
                    dismiss();
                }

            }
        });
    }
}
